package com.filefilter.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * \* Created with IntelliJ IDEA.
 * \* Author: fly
 * \* Date: 2020/6/14
 * \* Time: 19:56
 * \* Description:
 * \
 */
@Configuration
@EnableAsync
public class AsyncConfig implements AsyncConfigurer {

    @Bean("taskExecutor")
    public Executor taskExecutor(){

        ThreadPoolTaskExecutor   executor = new ThreadPoolTaskExecutor();
        //  设置线程核心容量
        executor.setCorePoolSize(10);
        // 设置线程最大容量
        executor.setMaxPoolSize(20);
        // 设置队列长度
        executor.setQueueCapacity(200);
        //  设置线程超时时间
        executor.setKeepAliveSeconds(60);
        //  设置线程名称前缀
        executor.setThreadNamePrefix("taskThread - -");
        // 设置任务丢弃后使用策略
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());

        return  executor;
    }


}

